﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;
using LTP.Accounts.Bus;
namespace lcps.Web.projectinfo
{
    public partial class List : Page
    {
        
        
        
		lcps.BLL.projectinfo bll = new lcps.BLL.projectinfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["login"] == null)
                {
                    //Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请先登陆！", "Default.aspx");
                    Maticsoft.Common.MessageBox.ResponseScript(this, "top.location.href='../Default.aspx'");
                }
                else
                {
                    gridView.BorderColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_bordercolorlight"].ToString());
                    gridView.HeaderStyle.BackColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString());
                    btnDelete.Attributes.Add("onclick", "return confirm(\"你确认要删除吗？\")");
                    BindData();
                }
              
            }
        }
        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
        
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0) 
                return;
            bll.DeleteList(idlist);
            BindData();
        }
        
        #region gridView
                        
        public void BindData()
        {
            #region
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[7].Visible = true;
            //}
            #endregion

            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat("purchaseid like '%{0}%'", txtKeyword.Text.Trim());
            }            
            ds = bll.GetList(strWhere.ToString());            
            gridView.DataSource = ds;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");

                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    string str = ds.Tables[0].Rows[0]["userrank"].ToString();//获取用户权限
                    HyperLink hy_edit = e.Row.FindControl("hy_edit") as HyperLink;//获取编辑按钮

                    if (!string.IsNullOrEmpty(str))
                    {
                        string[] tableIds = str.Split('|');

                        Label lbl_tableid = e.Row.FindControl("lbl_tableid") as Label;//获取项目类型

                        foreach (string tableid in tableIds)
                        {
                            if (tableid == lbl_tableid.Text)
                            {
                                hy_edit.Enabled = true;
                            }
                        }

                    }
                    else
                    {
                        hy_edit.Enabled = true;
                    }

                }
                else
                {
                    Maticsoft.Common.MessageBox.ResponseScript(this, "top.location.href='../Default.aspx'");
                }
               
            }
        }
        
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {                        
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

        protected void btn_excel_Click(object sender, EventArgs e)
        {
            this.gridView.AllowPaging = false;

            if (this.gridView.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.Year + "lyb.xls");
                Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>"+DateTime.Now.Year+"年计划履约明细表</H4>");
                Response.Output.Write("<H5 align='left'>共有条" + this.gridView.Rows.Count + "记录</H5>");
                this.gridView.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }
        }

        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }






    }
}
